package top.xqf.homework.service.impl;

import org.springframework.stereotype.Service;
import top.xqf.homework.base.BaseResult;
import top.xqf.homework.dao.TbXqfGoodsDAO;
import top.xqf.homework.entity.TbXqfGoods;
import top.xqf.homework.service.GoodsService;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    private final TbXqfGoodsDAO tbXqfGoodsDAO;

    public GoodsServiceImpl(TbXqfGoodsDAO tbXqfGoodsDAO) {
        this.tbXqfGoodsDAO = tbXqfGoodsDAO;
    }

    @Override
    public BaseResult<List<TbXqfGoods>> queryAll() throws Exception{
        BaseResult<List<TbXqfGoods>> result=new BaseResult<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage("");
        result.setData(tbXqfGoodsDAO.queryAll(new TbXqfGoods()));
        return result;
    }


    @Override
    public BaseResult<TbXqfGoods> add(TbXqfGoods tbXqfGoods) throws Exception{
        BaseResult<TbXqfGoods> result=new BaseResult<>();
        int n=tbXqfGoodsDAO.add(tbXqfGoods);
        result.setCode(n==1?200:500);
        result.setSuccess(n==1);
        result.setMessage(n==1?"添加成功":"添加失败");
        result.setData(tbXqfGoods);
        return result;
    }

    @Override
    public BaseResult<TbXqfGoods> update(TbXqfGoods tbXqfGoods) throws Exception{
        BaseResult<TbXqfGoods> result=new BaseResult<>();
        int n=tbXqfGoodsDAO.update(tbXqfGoods);
        result.setCode(n==1?200:500);
        result.setSuccess(n==1);
        result.setMessage(n==1?"修改成功":"修改失败");
        result.setData(tbXqfGoods);
        return result;
    }

    @Override
    public BaseResult<TbXqfGoods> delete(TbXqfGoods tbXqfGoods) throws Exception{
        BaseResult<TbXqfGoods> result=new BaseResult<>();
        int n=tbXqfGoodsDAO.delete(tbXqfGoods);
        result.setCode(n==1?200:500);
        result.setSuccess(n==1);
        result.setMessage(n==1?"删除成功":"删除失败");
        result.setData(tbXqfGoods);
        return result;
    }
}
